﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Articles : Form
    {
        public Articles()
        {
            InitializeComponent();
        }

        private void Articles_Load(object sender, EventArgs e)
        {
            var table = "select * from Articles";

            var tablefriend = "select * from Users";

            var dataTable = DbHelper.GetDataTable(tablefriend);

            dataGridView1.DataSource = dataTable;

            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.AllowUserToAddRows = false;
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var table = string.Format("select * from Users where UserName like '%{0}%'", name);

            var dataTable = DbHelper.GetDataTable(table);

            dataGridView1.DataSource = dataTable;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {   
            Add add = new Add();    

            var table=add.ShowDialog();

            if (table == DialogResult.Yes)
            {
                var name = textBox1.Text;

                var tablefriend = string.Format("select * from Users where UserName like '%{0}%'", name);

                var dataTable = DbHelper.GetDataTable(tablefriend);

                dataGridView1.DataSource = dataTable;
            }
            else
            {
                MessageBox.Show("Be Defeated", "提示");
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["UserName"].Value;
                var passWord = (string)dataGridView1.SelectedRows[0].Cells["PassWord"].Value;

                Add add = new Add(id,name,passWord);

                var table= add.ShowDialog();

                if (table == DialogResult.Yes)
                {
                    var nameFriend = textBox1.Text;

                    var tablefriend = string.Format("select * from Users where UserName like '%{0}%'", nameFriend);

                    var dataTable = DbHelper.GetDataTable(tablefriend);

                    dataGridView1.DataSource = dataTable;
                }
                else
                {
                    MessageBox.Show("Be Defeated", "提示");
                }
            }
            else
            {
                MessageBox.Show("您没选择行", "提示");
            }

          


        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)

        {
            if (dataGridView1.SelectedRows.Count > 0){

                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var delete = MessageBox.Show("确认删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (delete.Equals(DialogResult.Yes))
                {
                    var table = string.Format("delete from Users where Id={0}", id);
                    var rowCount = DbHelper.AddOrDelete(table);
                    if (rowCount > 0)
                    {
                        var name = textBox1.Text;

                        var tablefriend = string.Format("select * from Users where UserName like '%{0}%'", name);

                        var dataTable = DbHelper.GetDataTable(tablefriend);

                        dataGridView1.DataSource = dataTable;

                        MessageBox.Show("删除成功", "提示");
                    }
                }
                else
                {
                    MessageBox.Show("没有选择行", "提示");
                }
            }


        
        }
    }
}
